package wallpaperSetting;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.stage.Stage;
import utils.fileHandler.FileHandler;

import java.net.URL;
import java.util.ResourceBundle;

public class WallpaperSettingCtrl implements Initializable
{
    public Label transparentRateLabel;
    public ComboBox<String> switchComboBox;
    public TextField hourTextField;
    public Button saveButton;
    public Button cancelButton;

    public Slider lightSlider;
    public ProgressBar lightVolumeProgressLeft;
    public ProgressBar lightVolumeProgressRight;
    public TextField minuteTextField;
    public ComboBox<String> audioComboBox;
    private double soundValue;
    private double transparency;

    public void save(){
        String minuteTextFieldText = minuteTextField.getText();
        String hourTextFieldText = hourTextField.getText();
        long time=(int)(Double.parseDouble(minuteTextFieldText)*60000)+(int)(Double.parseDouble(hourTextFieldText)*3600000);
        soundValue=audioComboBox.getSelectionModel().getSelectedIndex();
        System.out.println(time);
        FileHandler.setIntervalTime(time);
        FileHandler.setTransparency(transparency);
        FileHandler.setAudio(soundValue);
        Stage window = (Stage) cancelButton.getScene().getWindow();
        window.close();
    }

    public void cancel(){
        Stage window = (Stage) cancelButton.getScene().getWindow();
        window.close();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        switchComboBox.getItems().add("按顺序播放");
        switchComboBox.getSelectionModel().select(0);
        soundValue=FileHandler.getAudio();
        transparency= FileHandler.getTransparency();
        long intervalTime = FileHandler.getIntervalTime();
        minuteTextField.setText(String.valueOf(intervalTime%3600000/60000));
        hourTextField.setText(String.valueOf(intervalTime/3600000));
        transparentRateLabel.setText((int)transparency*100+"%");
        audioComboBox.getItems().add("关闭");
        audioComboBox.getItems().add("打开");
        if (FileHandler.getAudio()==0){
            audioComboBox.getSelectionModel().select(0);
        }else {
            audioComboBox.getSelectionModel().select(1);
        }
        lightSlider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
                double value = lightSlider.getValue();
                double max = lightSlider.getMax();
                lightVolumeProgressLeft.setProgress(1);
                lightVolumeProgressRight.setProgress(1);
                lightVolumeProgressLeft.setMinWidth(2*value);
                lightVolumeProgressLeft.setMaxWidth(value);
                lightVolumeProgressRight.setMinWidth((max - value)*2);
                lightVolumeProgressRight.setMaxWidth((max - value)*2);
                transparentRateLabel.setText((int)(value/max*100) + "%");
                transparency=value/max;
            }
        });
        lightSlider.setValue(transparency*lightSlider.getMax());
    }
}
